define(["jquery","servers"], function ($,servers) {
    return Backbone.Model.extend({
        defaults: function () {
            return {
            };
        },
        getPageList:function(param){
            var self = this;
            var paramMap=servers.getPageParamMap();
            paramMap= $.ExtCommon.initMergeJson(paramMap,{adveristerId:"customer",status:"status"});
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.crowd.pageList(newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        var newInfo= $.ExtCommon.getMappingJson(info,{
                            id:"id",name:"name",type:"type",createTime:"createTime",customerName:"companyName",
                            statusName:"status",coverCount:"userCount"
                        },true);
                        if(!$.ExtCommon.isCheckEmpty(newInfo.createTime)){newInfo.createTime=$.ExtMoment.initFormat({value:newInfo.createTime,format:"YYYY-MM-DD HH:mm:ss"})}
                        var mapType=servers.base.getMapCrowdType(newInfo.type);
                        newInfo.type=mapType.idMap;
                        newInfo.typeName=mapType.name;
                        if($.ExtCommon.isCheckUndefined(newInfo.typeName)){newInfo.typeName=""}
                        newInfo.isCustom=newInfo.type==="1";
                        newInfo.isSimilar=newInfo.type==="2";
                        return newInfo;
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataPageList: res});
                self.trigger('bind:PageList');
            });
        },
        getCustomList:function(){
            var self = this;
            servers.crowd.customList(function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data,function (info) {
                        return $.ExtCommon.getMappingJson(info,{id:"id",name:"name",statusName:"statusName",coverCount:"number"},true);
                    });
                }else{
                    res.data=[];
                }
                res.dataOriginal=res.data;
                res.data=newJson;
                self.set({dataCustomList: res});
                self.trigger('bind:CustomList');
            });
        },
        deleteInfo: function (param) {
            var self = this;
            var newParam=$.ExtCommon.getMappingJson(param, {id:"id",type:"type"});
            servers.crowd.delete(newParam,function (res) {
                self.set({dataDeleteInfo: res});
                self.trigger('delete:Info');
            });
        },

        getInfoCustom: function (id) {
            var self = this;
            if (id) {
                servers.crowd.customInfo({id:id}, function (res) {
                    var newJson = $.ExtCommon.getMappingJson(res.success?res.data:{}, {
                       id:"id",name:"name",description:"description",bagType:"codeNumType",customer:"advertiserId",fileUrl:"url"
                    },true);
                    newJson.fileData=$.ExtCommon.isCheckEmpty(newJson.fileUrl)?{}:{relativePath:newJson.fileUrl,path:newJson.fileUrl,uploadId:newJson.fileUrl};
                    res.dataOriginal=res.data;
                    res.data=newJson;
                    self.set({dataCustomInfo: {isAdd:false,res:res}});
                    self.trigger('bind:CustomInfo');
                });
            } else {
                var dataDef={fileData:{}};
                self.set({dataCustomInfo:{isAdd:true,res:servers.getSendSuccess(dataDef)}});
                self.trigger('bind:CustomInfo');
            }
        },
        addInfoCustom: function (json) {
            var self = this;
            var data=$.ExtCommon.getMappingJson(json, {
                name:"name",description:"description",codeNumType:"bagType",advertiserId:"customer",path:"filePath",relativePath:"fileUrl",uploadId:"fileId"
            });
            servers.crowd.addCustom(data, function (res) {
                self.set({dataSaveCustom: {isAdd: true, req: json, res: res}});
                self.trigger('save:CustomInfo');
            });
        },
        updateInfoCustom: function (json) {
            var self = this;
            var data=$.ExtCommon.getMappingJson(json, {id:"id",name:"name",advertiserId:"customer",description:"description"});
            servers.crowd.updateCustom(data, function (res) {
                self.set({dataSaveCustom: {isAdd: false, req: json, res: res}});
                self.trigger('save:CustomInfo');
            });
        },

        getInfoSimilar: function (id) {
            var self = this;
            if (id) {
                servers.crowd.similarInfo({id:id}, function (res) {
                    var newJson = $.ExtCommon.getMappingJson(res.success?res.data:{}, {
                        id:"id",name:"name",description:"description",customName:"seedName",coverCount:"number",customStatus:"status",num:"expandUserCount"
                    },true);
                    newJson.customInfo={name:newJson.customName,statusName:newJson.customStatus,coverCount:newJson.coverCount};
                    res.dataOriginal=res.data;
                    res.data=newJson;
                    self.set({dataSimilarInfo: {isAdd:false,res:res}});
                    self.trigger('bind:SimilarInfo');
                });
            } else {
                var dataDef={};
                self.set({dataSimilarInfo:{isAdd:true,res:servers.getSendSuccess(dataDef)}});
                self.trigger('bind:SimilarInfo');
            }
        },
        addInfoSimilar: function (json) {
            var self = this;
            var data=$.ExtCommon.getMappingJson(json, {
                name:"name",description:"description",seedAudienceId:"custom",expandUserCount:"num"
            });
            servers.crowd.addSimilar(data, function (res) {
                self.set({dataSaveSimilar: {isAdd: true, req: json, res: res}});
                self.trigger('save:SimilarInfo');
            });
        },
        updateInfoSimilar: function (json) {
            var self = this;
            var data=$.ExtCommon.getMappingJson(json, {id:"id",name:"name",description:"description"});
            servers.crowd.updateSimilar(data, function (res) {
                self.set({dataSaveSimilar: {isAdd: false, req: json, res: res}});
                self.trigger('save:SimilarInfo');
            });
        }
    });
});